<template>
  <div>
    <van-nav-bar title="爬楼费修改" left-text="返回" left-arrow @click-left="leftClick" />
    <van-cell-group>
      <van-field v-model="form.name" label="名称" clearable v-validate="'required'" data-vv-name="名称"
        :error-message="errors.first('名称')" placeholder="请输入名称" required maxLength="300" />
      <van-field v-model="form.price" label="价格" clearable v-validate="'required|price'" data-vv-name="价格"
        :error-message="errors.first('价格')" placeholder="请输入价格" required />
      <van-field v-model="form.stock" label="库存" clearable v-validate="'required'" data-vv-name="库存"
        :error-message="errors.first('库存')" placeholder="请输入库存" required />
      <van-field v-model="form.description" label="描述" clearable v-validate="'required'" data-vv-name="描述"
        :error-message="errors.first('描述')" type="textarea" rows="5" required />
    </van-cell-group>
    <van-button class="bottom-btn" size="large" type="info" @click="save">保存</van-button>
  </div>
</template>

<script>
  import {
    PRODUCT_DETAIL,
    PRODUCT_EDIT
  } from '@/api/product'
  import UploadPic from '@/components/UploadPic'
  export default {
    components: {
      UploadPic
    },
    data() {
      return {
        form: {},
      }
    },
    mounted() {
      this.getDetail();
    },
    methods: {
      leftClick() {
        this.$router.go(-1)
      },
      //获取详情
      getDetail() {
        this.$reqGet(PRODUCT_DETAIL, {
          productId: this.$route.params.id,
          own: this.$route.params.own
        }).then(res => {
          let info = res.data.sProduct;
          this.form = info;
        });
      },
      //保存
      save() {
        this.form.deliver = 1
        this.$validator.validateAll().then((result) => {
          if (result) {
            this.$dialog.confirm({
              title: '提示',
              message: '修改后商品需管理员重新审核，确定修改？'
            }).then(() => {
              this.$reqPost(PRODUCT_EDIT, this.form).then(res => {
                this.$toast('修改成功');
                this.$router.go(-1);
              }).catch(err => {
                this.$toast('修改失败');
              });
            }).catch(() => {
              // on cancel
            });
          }
        });
      }
    }
  }

</script>
